package com.heima.controller;


import cn.hutool.core.bean.BeanUtil;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.common.PageResponse;
import com.heima.entity.TbNode;
import com.heima.entity.TbRegion;
import com.heima.mapper.TbNodeMapper;
import com.heima.mapper.TbRegionMapper;
import com.heima.pojo.dto.TbRegionDto;
import com.heima.pojo.vo.TbNodeVo;
import com.heima.pojo.vo.TbRegionVo;
import com.heima.service.ITbBusinessService;
import com.heima.service.ITbNodeService;
import com.heima.service.ITbRegionService;
import com.heima.util.NoProcessing;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 运营区域 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-06-08
 */
@RestController
@Slf4j
@RequestMapping("/api/vm-service")
public class TbRegionController {

    @Autowired
    private ITbRegionService iTbRegionService;

    @Autowired
    private TbRegionMapper tbRegionMapper;

    @Autowired
    private ITbNodeService iTbNodeService;

    @Autowired
    private TbNodeMapper tbNodeMapper;

    @Autowired
    private ITbBusinessService iTbBusinessService;

    @GetMapping("/region/search")
    public PageResponse<TbRegionVo> selectRegion(@RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                                 @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                                 @RequestParam(value = "name",required = false) String name){
        //1.构建分页参数
        Page<TbRegion> page = new Page<>(pageIndex,pageSize);

        //2.构建执行条件
        QueryWrapper<TbRegion> wrapper = new QueryWrapper<>();
        if (name != null) {
            wrapper.like("name",name);
        }
        //3.执行分页查询
        iTbRegionService.page(page,wrapper);
        List<TbRegionVo> tbRegionVos = page.getRecords().stream().map(r -> {
            Long id = r.getId();
            Integer count = iTbNodeService.lambdaQuery()
                    .eq(id != null, TbNode::getRegionId, id)
                    .count();
            List<TbNode> nodeList = iTbNodeService.lambdaQuery()
                    .eq(id != null, TbNode::getRegionId, id)
                    .list();
            TbRegionVo tbRegionVo = new TbRegionVo();
            BeanUtil.copyProperties(r, tbRegionVo);
            tbRegionVo.setNodeCount(count);
            tbRegionVo.setNodeList(nodeList);
            return tbRegionVo;
        }).collect(Collectors.toList());

        //封装并返回结果
        PageResponse<TbRegionVo> response = new PageResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount((int) page.getTotal());
        response.setCurrentPageRecords(tbRegionVos);
        return response;
    }

    @PostMapping("/region")
    public Boolean addRegion(@RequestBody TbRegionDto dto){
        log.info("新增点位,{}",dto);
        TbRegion tbRegion1 = new TbRegion();
        tbRegion1.setName(dto.getRegionName());
        tbRegion1.setRemark(dto.getRemark());
        int row = tbRegionMapper.insert(tbRegion1);
        if (row > 0) {
            return true;
        }
        return false;
    }
    @PutMapping("/region/{id}")
    public Boolean updateRegion(@PathVariable("id") String id,
                                @RequestBody TbRegionDto dto){
        long rId = Long.parseLong(id);
        TbRegion tbRegion = new TbRegion();
        tbRegion.setId(rId);
        tbRegion.setName(dto.getRegionName());
        tbRegion.setRemark(dto.getRemark());
        int row = tbRegionMapper.updateById(tbRegion);
        if (row > 0) {
            return true;
        }
        return false;
    }
//    @GetMapping("/node/search")
    public PageResponse<TbNodeVo> getRegion(@RequestParam(value = "pageIndex",required = false) Integer pageIndex,
                                            @RequestParam(value = "pageSize",required = false) Integer pageSize,
                                            @RequestParam(value = "regionId",required = false) String regionId){
        //1.设置分页
        Page<TbNode> page = new Page<>(pageIndex,pageSize);
        //2.构造条件参数
        String regId = NoProcessing.processString(regionId);
        long l = Long.parseLong(regId);
        //执行分页查询
        LambdaQueryWrapper<TbNode> wrapper = new QueryWrapper<TbNode>()
                .lambda().like(regId != null, TbNode::getRegionId, l);
        Page<TbNode> page1 = iTbNodeService.page(page, wrapper);

        List<TbNodeVo> tbNodeVoList = page1.getRecords().stream().map(r -> {
            Integer count = iTbNodeService.lambdaQuery()
                    .like(r != null, TbNode::getRegionId, r)
                    .count();
            TbNodeVo tbNodeVo = new TbNodeVo();
            tbNodeVo.setName(r.getName());
            tbNodeVo.setId(Long.valueOf(r.getId()));
            tbNodeVo.setVmCount(count);
            return tbNodeVo;
        }).collect(Collectors.toList());




        //封装并返回结果
        PageResponse<TbNodeVo> response = new PageResponse<>();
        response.setPageIndex(pageIndex);
        response.setPageSize(pageSize);
        response.setTotalCount((int) page.getTotal());
        response.setCurrentPageRecords(tbNodeVoList);

        return response;
    }

    @DeleteMapping("/region/{id}")
    public Boolean deleteRegion(@PathVariable("id") Integer id){

        QueryWrapper<TbRegion> wrapper = new QueryWrapper<>();
        wrapper.like("id",id);

        int row = tbRegionMapper.delete(wrapper);
        if (row > 0) {
            return true;
        }
        return false;
    }
}
